<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiModelBuilder
{
	private static $_config;
	
	private static function _getPhpClassTemplate()
	{
		return <<<MODEL_CLASS
<?php if (! defined('APPPATH')) die('No direct script access allowed');
/*
 *------------------------------------------------------------------------------
 * Model Class %s
 *------------------------------------------------------------------------------
 * Auto-build by RtiModelBuilder
 * http://renarti.com/php-framework/modelbuilder 
*/

class %s extends RtiModel
{
%s

	public function getTableName()
	{
		return '%s';
	}

	public function getFields()
	{
		return array(%s);
	}

	public function getPK()
	{
		return array(%s);
	}	
}

/* End of file %s */
/* Location: %s */
MODEL_CLASS;
	}
	
	public static function build($alias = 'default')
	{
		Rti::loadConfig('database');
		
		self::$_config = Rti::config('database')->get($alias);
		
		if (empty(self::$_config))
		{
			Rti::showError("Undefined database alias [$alias]");
		}
		
		$type = trim(self::$_config[0]);
		
		switch ($type)
		{
			case "mysql": 
				self::_prepareMySql($alias);
				break;
		}
	}
		
	private static function _prepareMySql($alias)
	{
		$dbname = self::$_config[2];
		
		// tables
		$tables = Rti::db($alias)->query("SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA=?", array($dbname))->rows();

		// columns
		foreach ($tables as $table)
		{
			$table->COLUMNS = Rti::db($alias)->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? and TABLE_NAME=? ORDER BY ORDINAL_POSITION", array($dbname, $table->TABLE_NAME))->rows();
		}
		
		// Table2Class
		foreach ($tables as $table)
		{
			// original table name
			$table_name = $table->TABLE_NAME;
			
			// Normalize table/class name
			$class_name = explode('_', $table_name);
			foreach ($class_name as $i => $v) 
			{
				$class_name[$i] = ucfirst($v);
			}
			$class_name = implode('', $class_name);
			
			// class properties
			$fields       = array();
			$properties   = array();
			$primary_keys = array();
			
			// property template
			$txt_property = "\tpublic \$%s; // %s, NULLABLE %s, DEFAULT %s, %s";
			
			// itterate each field
			foreach ($table->COLUMNS as $column)
			{
				$properties[] = sprintf($txt_property, $column->COLUMN_NAME, $column->COLUMN_TYPE, $column->IS_NULLABLE, $column->COLUMN_DEFAULT, $column->COLUMN_COMMENT);
				
				$fields[] = "'" . $column->COLUMN_NAME . "'";
				
				if ($column->COLUMN_KEY == 'PRI')
				{
					$primary_keys[] = "'" . $column->COLUMN_NAME . "'";
				}
			}
			
			// path
			$class_path = APPPATH . 'models/' . $alias . '/' . $class_name . '.php';
			
			// template
			$php_class = self::_getPhpClassTemplate();
			$php_class = sprintf($php_class, 
										$class_name, 
										$class_name, 
										implode("\n", $properties), 
										$table_name, 
										implode(",", $fields), 
										implode(",", $primary_keys),
										$class_name . '.php',
										'./application/models/' . $alias . '/' . $class_name . '.php');
			
			file_put_contents($class_path, $php_class);						
		}
		
		echo "<h1>Done at " . date('Y-m-d H:i:s') . "</h1>";
	}
}


/* End of file RtiModelBuilder.php */
/* Location: ./system/helpers/RtiModelBuilder.php */
